package com.school.edupoint.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.school.edupoint.config.EduContext;
import com.school.edupoint.mapper.GiftMapper;
import com.school.edupoint.mapper.StudentGiftMapper;
import com.school.edupoint.mapper.StudentMapper;
import com.school.edupoint.model.Student;
import com.school.edupoint.model.StudentGift;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class StudentGiftService {
    @Autowired
    private EduContext eduContext;
    @Autowired
    private StudentGiftMapper studentGiftMapper;
    @Autowired
    private StudentMapper studentMapper;
    @Autowired
    private GiftMapper giftMapper;


    public IPage<StudentGift> selectPage(String title, int pageNum, int pageSize, Integer status, String studentName) {

        // 创建分页对象
        Page<StudentGift> page = new Page<>(pageNum, pageSize);
        // 构建查询条件
        LambdaQueryWrapper<StudentGift> wrapper = new LambdaQueryWrapper<>();
        // 如果 title 有值，则加上模糊查询条件
        if (title != null && !title.isEmpty()) {
            wrapper.like(StudentGift::getGiftName, title);
        }
        if (studentName != null && !studentName.isEmpty()) {
            wrapper.like(StudentGift::getStudentName, studentName);
        }
        if (status != null) {
//        if (status != null && !status.isEmpty()) {
            wrapper.like(StudentGift::getStatus, status);
        }
        // 统一按创建时间降序排序
        wrapper.orderByDesc(StudentGift::getCreateTime);

        return studentGiftMapper.selectPage(page, wrapper);
    }

    /**
     * 根据ID获取兑换记录
     *
     * @param id
     * @return
     */
    public StudentGift getById(Integer id) {
        return studentGiftMapper.selectById(id);
    }

    /**
     * 已发货
     *
     * @param studentGiftId
     * @return
     */
    public String delivery(Integer studentGiftId) {
        LambdaQueryWrapper<StudentGift> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StudentGift::getId, studentGiftId);
        StudentGift studentGift = studentGiftMapper.selectOne(queryWrapper);
        studentGift.setStatus(2);
        studentGiftMapper.updateById(studentGift);
        return "已发货";
    }

    /**
     * 已送达
     *
     * @param studentGiftId
     * @return
     */
    public String over(Integer studentGiftId) {
        LambdaQueryWrapper<StudentGift> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StudentGift::getId, studentGiftId);
        StudentGift studentGift = studentGiftMapper.selectOne(queryWrapper);
        studentGift.setStatus(3);
        studentGiftMapper.updateById(studentGift);
        return "已送达";
    }

    /**
     * 已取消
     *
     * @param studentGiftId
     * @return
     */
    public String cancel(Integer studentGiftId) {
//        更改状态
        LambdaQueryWrapper<StudentGift> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StudentGift::getId, studentGiftId);
        StudentGift studentGift = studentGiftMapper.selectOne(queryWrapper);
        studentGift.setStatus(4);
        studentGiftMapper.updateById(studentGift);
//        返回积分
        studentMapper.updatePonint(studentGift.getStudentId(),studentGift.getPoint());
//        返回库存
        String giftName = studentGift.getGiftName();
        giftMapper.updateByGiftName(giftName);
        return "已取消";
    }

    public IPage<StudentGift> getStudentGiftInfo(String title, int pageNum, int pageSize) {
        Integer studentId = eduContext.getStudent().getId();
        // 创建分页对象
        Page<StudentGift> page = new Page<>(pageNum, pageSize);
        // 构建查询条件
        LambdaQueryWrapper<StudentGift> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(StudentGift::getStudentId, studentId);
        // 如果 title 有值，则加上模糊查询条件
        if (title != null && !title.isEmpty()) {
            wrapper.like(StudentGift::getGiftName, title);
        }

        // 统一按创建时间降序排序
        wrapper.orderByDesc(StudentGift::getCreateTime);

        return studentGiftMapper.selectPage(page, wrapper);

    }
}
